package com.boss.salary.dao.impl;

import com.boss.framework.dao.BaseDaoImpl;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.constant.TableName;
import com.boss.salary.dao.IOperrateLogsDao;
import com.boss.salary.entity.OperrateLogs;
import com.boss.sys.utils.SysUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

@Repository
public class OperrateLogsDaoImpl extends BaseDaoImpl  implements IOperrateLogsDao{
    //当月时间
    String TimeNow = new SimpleDateFormat("yyyy-MM ").format(Calendar.getInstance().getTime());
    @Override
    public List<OperrateLogs> queryOperatelogsPage(PaginationDTO paginationDTO,Map<String, Object> queryParam,List<String> childrenId) {
        StringBuilder sql = new StringBuilder();
        List<Object> Params = new ArrayList<>();
        sql.append(" select id, biz_key, create_time, update_time, year, uesername, token_id,")
           .append(" operate_name, agency_id, person_id, salary_id, is_deleted, mof_div_code,user_agency_id from ")
           .append(TableName.SAL_OPERRATE_LOGS);
        sql.append(" where  mof_div_code=?");
        Params.add(queryParam.get("mofDivCode"));
        if(!StringUtils.isEmpty(queryParam.get("endDate"))){
            sql.append(" and  create_time<= ?");
            Params.add(queryParam.get("endDate"));
        }
        if(!StringUtils.isEmpty(queryParam.get("startDate"))){
            sql.append(" and  create_time>= ?");
            Params.add(queryParam.get("startDate"));
        }
        if(!StringUtils.isEmpty(queryParam.get("ueserName"))){
            sql.append(" and  uesername like ?");
            Params.add('%'+queryParam.get("ueserName").toString()+'%');
        }
        if(!StringUtils.isEmpty(queryParam.get("operateName"))){
            sql.append(" and  operate_name like ?)");
            Params.add('%'+queryParam.get("operateName").toString()+'%');
        }
        if(StringUtils.isEmpty(queryParam.get("endDate"))&&StringUtils.isEmpty(queryParam.get("startDate"))){
            sql.append(" and  SUBSTRING(create_time,1,7)=?");
            Params.add(TimeNow);
        }
        sql.append(" and  ").append(SysUtils.inSql("user_agency_id",childrenId));
        sql.append(" order by  create_time desc ");
        String querySql = this.boxUpSql(sql.toString());
        //请求参数初始化
        Object[] params = this.boxUpParam(Params.toArray(), paginationDTO);
        return this.getJdbcTemplate().query(querySql, new BeanPropertyRowMapper(OperrateLogs.class), params);
    }

    @Override
    public int queryOperatelogsTotal(Map<String, Object> queryParam, List<String> childrenId) {
        StringBuilder sql = new StringBuilder();
        List<Object> Params = new ArrayList<>();
        sql.append("select  count(*) from  ").append(TableName.SAL_OPERRATE_LOGS);

        sql.append(" where    mof_div_code=? ");
        Params.add(queryParam.get("mofDivCode"));
        if(!StringUtils.isEmpty(queryParam.get("endDate"))){
            sql.append(" and  create_time<= ?");
            Params.add(queryParam.get("endDate"));
        }
        if(!StringUtils.isEmpty(queryParam.get("startDate"))){
            sql.append(" and  create_time>= ?");
            Params.add(queryParam.get("startDate"));
        }
        if(!StringUtils.isEmpty(queryParam.get("ueserName"))){
            sql.append(" and  uesername like ? ");
            Params.add('%'+queryParam.get("ueserName").toString()+'%');
        }
        if(!StringUtils.isEmpty(queryParam.get("operateName"))){
            sql.append(" and  operate_name like ? ");
            Params.add('%'+queryParam.get("operateName").toString()+'%');
        }
        if(StringUtils.isEmpty(queryParam.get("endDate"))&&StringUtils.isEmpty(queryParam.get("startDate"))){
            sql.append(" and  SUBSTRING(create_time,1,7)=?");
            Params.add(TimeNow);
        }
        sql.append(" and ").append(SysUtils.inSql("user_agency_id",childrenId));

        return this.getJdbcTemplate().queryForObject(sql.toString(),Integer.class,Params.toArray());
    }

    @Override
    public void saveOperrateLogs(OperrateLogs operrateLogs) {
        StringBuilder sql=new StringBuilder();
        sql.append("insert into ").append(TableName.SAL_OPERRATE_LOGS);
        sql.append(" (id, biz_key, create_time, update_time, year, uesername, token_id,")
                .append(" operate_name, agency_id, person_id, salary_id, is_deleted, mof_div_code,user_agency_id)")
                .append(" values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)") ;
        Object[] params = new Object[]{
                operrateLogs.getId(),operrateLogs.getBizKey(),operrateLogs.getCreateTime(),operrateLogs.getUpdateTime(),operrateLogs.getYear(),
                operrateLogs.getUeserName(),operrateLogs.getTokenId(),operrateLogs.getOperateName(),operrateLogs.getAgencyId(),
                operrateLogs.getPersonId(),operrateLogs.getSalaryId(),operrateLogs.getIsDeleted(),operrateLogs.getMofDivCode(),operrateLogs.getUserAgencyId()
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }
}
